<template>
  <div class="dingbu">
    <!-- 照片 -->
    <div class="tupian">
      <van-image
        fit="cover"
        round
        width="64"
        height="64"
        :src="userInfo.photo"
      />
      <h2 class="wenzi">
        {{ userInfo.name }}
        <br />
        <van-tag color="#fff" text-color="#0098fa">{{
          userInfo.birthday
        }}</van-tag>
      </h2>
    </div>
    <!-- 操作 -->
    <div class="caozuo">
      <van-row icon>
        <van-col span="8">
          <van-icon name="newspaper-o" color="#7af" />
          我的作品</van-col
        >
        <van-col span="8">
          <van-icon name="star-o" color="#f00" />
          我的收藏</van-col
        >
        <van-col span="8">
          <van-icon name="tosend" color="#fa0" />
          阅读历史</van-col
        >
      </van-row>
    </div>
    <!-- 单元格 -->
    <div class="cell">
      <van-cell-group inset class="user-group">
        <van-cell
          icon="edit"
          title="编辑资料"
          is-link
          @click="$router.push('/edit')"
        />
        <van-cell icon="chat-o" title="小智同学" is-link />
        <van-cell icon="setting-o" title="系统设置" is-link />
        <van-cell icon="warning-o" title="退出登录" is-link @click="loginout" />
      </van-cell-group>
    </div>
  </div>
</template>

<script>
//导入
import { getUser } from './user'
import { removeToken } from '@/utils/token'
export default {
  name: 'user',
  data () {
    return {
      userInfo: {}
    }
  },
  //生命钩子
  created () {
    getUser().then(res => {
      //console.log(res)
      this.userInfo = res.data.data
      //保存数据到vuex
      this.$store.commit('setUserInfo', res.data.data)
    })
  },
  methods: {
    loginout () {
      this.$dialog
        .confirm({
          message: '你确定要退出吗'
        })
        .then(() => {
          // on confirm
          //清除token
          removeToken()
          //清空数据
          this.userInfo = {}
          //跳转首页
          this.$router.push({ path: '/login' })
        })
        .catch(() => {
          // on cancel
        })
    }
  }
}
</script>

<style lang="less">
.dingbu {
  .tupian {
    height: 100px;
    background-color: #0098fa;
    display: flex;
    align-items: center;
    margin-bottom: 10px;
    .van-image {
      padding: 0 15px;
    }
    .wenzi {
      font-weight: normal;
      font-size: 16px;
      color: aliceblue;
    }
  }
  .caozuo {
    text-align: center;
    font-size: 16px;
    margin-bottom: 10px;
    .van-icon {
      display: block;
      font-size: 20px;
    }
  }
}
</style>
